package com.renrenyou.duanshipin.tools;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;

import com.bumptech.glide.Glide;
import com.renrenyou.duanshipin.R;
import com.renrenyou.duanshipin.base.BaseActivity;
import com.renrenyou.duanshipin.databinding.ActivityWeijinBinding;
import com.renrenyou.duanshipin.util.ProgressDialogUtil;
import com.renrenyou.duanshipin.util.ToastUtil;

public class WeijinActivity extends BaseActivity<ActivityWeijinBinding> implements View.OnClickListener {

    private String[] mWeijin = new String[]{"sb", "TMD", "傻逼", "色情", "暴力", "赌博"};

    @Override
    public ActivityWeijinBinding getBinding(LayoutInflater inflater) {
        return ActivityWeijinBinding.inflate(inflater);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Glide.with(this).load(R.mipmap.ic_material_bg).into(mBinding.ivWeijinBg);
        SpannableString sb = new SpannableString(getString(R.string.weijin_desc));
        sb.setSpan(new ForegroundColorSpan(Color.RED), 8, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        sb.setSpan(new ForegroundColorSpan(Color.parseColor("#FF9100")), 15, 17, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mBinding.tvWeijinDesc.setText(sb);

        mBinding.tvWeijinChange.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tv_weijin_change) {
            Editable editable = mBinding.edtWeijinContent.getText();
            if (editable != null) {
                String content = editable.toString().trim();
                if (TextUtils.isEmpty(content)) {
                    ToastUtil.showToast(WeijinActivity.this, "请输入要检测的内容");
                    return;
                }
                Handler handler = new Handler();
                AlertDialog dialog = ProgressDialogUtil.showProgressDialog(this);
                handler.postDelayed(() -> {
                    dialog.dismiss();
                    checkWeijin(content);
                }, 3000);
            }
        }
    }

    private void checkWeijin(String content) {
        SpannableString sb = new SpannableString(content);
        boolean hasWeijin = false;
        for (String key : mWeijin) {
            int index = content.indexOf(key);
            if (index != -1) {
                sb.setSpan(new ForegroundColorSpan(Color.RED), index, index + key.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                if (!hasWeijin) {
                    hasWeijin = true;
                }
            }
        }
        mBinding.tvWeijinContent.setText(hasWeijin ? sb : "暂未查询到");
        if (!hasWeijin) {
            ToastUtil.showToast(this, "暂未发现");
        }
    }
}
